program episodes_20_days
      character*6 ticker
      character*8 cusp
      integer(kind=8):: nshrs,ivol(5000),numshrs(5000)
      dimension jdate(5000),favg(5000),iperm(5000),price(5000),ishrout(5000),vwret(5000),retrn(5000),nep(5000),pctloan(5000)
      dimension chgloan(5000)
!      open(unit=10,file='e:\FIS_duration\episode_data.txt')
!      open(unit=99,file='e:\FIS_duration\ret_aft_lgchg_onloan.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\ret_aft_lgchg_onloan_rep.txt')
      iend=0
      j=0
1     j=j+1
      read(10,1000,end=900)nep(j),jast,jdate(j),cusp,ticker,itickt,numshrs(j),age,favg(j),fmax,fmin,fstd,iperm(j),price(j),ivol(j),retrn(j),ishrout(j),vwret(j),util,nshrs
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13)
      pctloan(j)=float(numshrs(j))/(1000.*float(ishrout(j)))
      if(nep(j).eq.nep(1)) go to 1
5     jcnt=j-1
      if(jcnt.lt.20) go to 500
      rcnt=float(jcnt)
      avgchg=0.
      ctpos=0.
      ctneg=0.
      chgmax=0.
      chgmin=999.
      retpos=0.
      retneg=0.
      ctpos2=0.
      ctneg2=0.
      retpos2=0.
      retneg2=0.
      ctpos5=0.
      ctneg5=0.
      retpos5=0.
      retneg5=0.
      ctlpos=0.
      retlpos=0.
      ctlpos2=0.
      retlpos2=0.
      ctlpos5=0.
      retlpos5=0.
      ctturn=0.
      turnpos=0.
      turnneg=0.
      turn2pos=0.
      turn2neg=0.
      turnlg=0.
      turnlpos=0.
      turn2lpos=0.
      retsdpos=0.
      retsdneg=0.
      retsdlpos=0.
      do 10 ii=3,jcnt
          jcntm4=jcnt-4
          im=ii-1
          im2=ii-2
          ip=ii+1
          ip2=ii+2
          ip3=ii+3
          ip4=ii+4
          chgloan(ii)=pctloan(im)-pctloan(im2)
          avgchg=avgchg+chgloan(ii)/(rcnt-2.)
          if(chgloan(ii).gt.0.) ctpos=ctpos+1.
          if(chgloan(ii).gt.0.02) ctlpos=ctlpos+1.
          if(chgloan(ii).lt.0.) ctneg=ctneg+1.
          if(chgloan(ii).gt.0.) retpos=retpos+retrn(ii)
          if(chgloan(ii).gt.0.) retsdpos=retsdpos+retrn(im)
          if(chgloan(ii).gt..02) retlpos=retlpos+retrn(ii)
          if(chgloan(ii).gt..02) retsdlpos=retsdlpos+retrn(im)
          if(chgloan(ii).lt.0.) retneg=retneg+retrn(ii)
          if(chgloan(ii).lt.0.) retsdneg=retsdneg+retrn(im)
          if(chgloan(ii).gt.chgmax) chgmax=chgloan(ii)
          if(chgloan(ii).lt.chgmin) chgmin=chgloan(ii)
          if(chgloan(ii).gt.0.) turnpos=turnpos+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).gt.0.02) turnlpos=turnlpos+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).lt.0.) turnneg=turnneg+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).gt.0.) turn2pos=turn2pos+float(ivol(ii))/(1000.*float(ishrout(ii)))
          if(chgloan(ii).gt.0.02) turn2lpos=turn2lpos+float(ivol(ii))/(1000.*float(ishrout(ii)))
          if(chgloan(ii).lt.0.) turn2neg=turn2neg+float(ivol(ii))/(1000.*float(ishrout(ii)))
          if(ii.eq.jcnt) go to 10
          if(chgloan(ii).gt.0.) ctpos2=ctpos2+1.
          if(chgloan(ii).gt.0.02) ctlpos2=ctlpos2+1.
          if(chgloan(ii).lt.0.) ctneg2=ctneg2+1.
          if(chgloan(ii).gt.0.) retpos2=retpos2+(1.+retrn(ii))*(1.+retrn(ip))-1.
          if(chgloan(ii).gt.0.02) retlpos2=retlpos2+(1.+retrn(ii))*(1.+retrn(ip))-1.
          if(chgloan(ii).lt.0.) retneg2=retneg2+(1.+retrn(ii))*(1.+retrn(ip))-1.
          if(ii.gt.jcntm4) go to 10
          if(chgloan(ii).gt.0.) ctpos5=ctpos5+1.
          if(chgloan(ii).gt.0.02) ctlpos5=ctlpos5+1.
          if(chgloan(ii).lt.0.) ctneg5=ctneg5+1.
          if(chgloan(ii).gt.0.) retpos5=retpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(ip4))-1.
          if(chgloan(ii).gt.0.02) retlpos5=retlpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(ip4))-1.
          if(chgloan(ii).lt.0.) retneg5=retneg5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(ip4))-1.
10    continue 
      if(ctpos.lt.1.) go to 15
      retpos=retpos/ctpos
      turnpos=turnpos/ctpos
      turn2pos=turn2pos/ctpos
      retsdpos=retsdpos/ctpos
15    if(ctneg.lt.1.) go to 20
      retneg=retneg/ctneg
      turnneg=turnneg/ctneg
      turn2neg=turn2neg/ctneg
      retsdneg=retsdneg/ctneg
20    continue 
      if(ctpos2.lt.1.) go to 25
      retpos2=retpos2/ctpos2
25    if(ctneg2.lt.1.) go to 30      
      retneg2=retneg2/ctneg2
30    continue  
      if(ctpos5.lt.1.) go to 35
      retpos5=retpos5/ctpos5
35    if(ctneg5.lt.1.) go to 40      
      retneg5=retneg5/ctneg5
40    continue      

      if(ctlpos.lt.1.) go to 50
      retlpos=retlpos/ctlpos
      turnlpos=turnlpos/ctlpos
      turn2lpos=turn2lpos/ctlpos
      retsdlpos=retsdlpos/ctlpos
50    continue 
      if(ctlpos2.lt.1.) go to 60
      retlpos2=retlpos2/ctlpos2
60    continue  
      if(ctlpos5.lt.1.) go to 70
      retlpos5=retlpos5/ctlpos5
70    continue      

      
      write(6,9900)nep(1),rcnt,ctpos,ctneg,chgmax,chgmin,avgchg,retpos,retneg,ctpos2,retpos2,ctneg2,retneg2,ctpos5,retpos5,ctneg5,retneg5,ctlpos,retlpos,ctlpos2,retlpos2,ctlpos5,retlpos5,turnpos,turn2pos,turnneg,turn2neg,turnlpos,turn2lpos,retsdpos,retsdneg,retsdlpos
      write(99,9900)nep(1),rcnt,ctpos,ctneg,chgmax,chgmin,avgchg,retpos,retneg,ctpos2,retpos2,ctneg2,retneg2,ctpos5,retpos5,ctneg5,retneg5,ctlpos,retlpos,ctlpos2,retlpos2,ctlpos5,retlpos5,turnpos,turn2pos,turnneg,turn2neg,turnlpos,turn2lpos,retsdpos,retsdneg,retsdlpos
9900  format(1x,i4,3(1x,f5.0),5(1x,f9.6),7(1x,f5.0,1x,f9.6),9(1x,f9.6))      
    

500   continue 
      nep(1)=nep(j)
      jdate(1)=jdate(j)
      numshrs(1)=numshrs(j)
      favg(1)=favg(j)
      iperm(1)=iperm(j)
      price(1)=price(j)
      ivol(1)=ivol(j)
      retrn(1)=retrn(j)
      ishrout(1)=ishrout(j)
      vwret(1)=vwret(j)
      j=1
      go to 1
900   continue    
      iend=iend+1
      if(iend.eq.1) go to 5
    end program episodes_20_days